package com.topdt.jbpm.dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;

import com.thx.common.orm.PaginationDao;
import com.thx.common.util.AppContext;
import com.topdt.jbpm.model.Flow;

/**
 *
 * 建立日期 : 2013-6-21 上午09:46:04<br>
 * 作者 : shiyan<br>
 * 模块 : <br>
 * 描述 : <br>
 * 修改历史: 序号 日期 修改人 修改原因 <br>
 * 1 <br>
 * 2 <br>
 */
@Repository
public class FlowDao extends PaginationDao<Flow> {

  public List<Flow> getAllByFlowName(String flowName) {
    String comId = AppContext.getCurrentComId();
    List<Object> params = new ArrayList<Object>();
    String hql = "from Flow f where f.companyId = ?";
    params.add(comId);
    if (StringUtils.isNotBlank(flowName)) {
      hql += " and f.flowName like ?";
      params.add("%" + flowName + "%");
    }
    hql += "  order by f.creatTime desc";
    return findPageList(hql, params.toArray());
  }

  @SuppressWarnings("unchecked")
  public List<Flow> getAllFillSelect() {
    String comId = AppContext.getCurrentComId();
    String sql =
        "select * from wf_flow flow where flow.company_id = ? and flow.jbpm_flow_id is not null  order by flow.creat_time desc";
    List<Flow> list = createSqlQuery(sql, new Object[] {comId}).addEntity(Flow.class).list();
    if (list.size() > 0)
      return list;
    return null;
  }

  /**
   * @Description : 根据流程名称查询列表
   * @Method_Name : findByName
   * @param flowName
   * @return
   * @return : List<Flow>
   * @Creation Date : 2015年3月23日 下午3:26:19
   * @version : v1.00
   * @Author : calonlan
   * 
   * @Update Date : 2015年3月23日 下午3:26:19
   * @Update Author : calonlan
   */
  @SuppressWarnings("unchecked")
  public List<Flow> findByName(String flowName) {
    String comId = AppContext.getCurrentComId();
    String hql = "from Flow f where f.companyId = ? and f.flowName = ?";
    return createQuery(hql, new Object[] {comId, flowName}).list();
  }

  @SuppressWarnings("unchecked")
  public List<Flow> queryAll(Flow entity, int start, int max) {
    String hql = "from Flow flow where flow.companyId = ?";
    List<Flow> list =
        createQuery(hql, new Object[] {entity.getCompanyId()}).setFirstResult(start)
            .setMaxResults(max).list();
    return list;
  }

  public int queryCount(Flow entity) {
    String sql = "select count(1) from wf_flow where COMPANY_ID = ?";
    Object result = createSqlQuery(sql, new Object[] {entity.getCompanyId()}).uniqueResult();
    if (result != null) {
      return Integer.parseInt(result.toString());
    }
    return 0;
  }

  @SuppressWarnings("unchecked")
  public Flow getById(String flowId) {
    String comId = AppContext.getCurrentComId();
    String condition = "";
    if (comId != null && !"".equals(comId))
      condition = "flow.companyId='" + comId + "' and ";
    String hql = "from Flow flow where " + condition + "flow.flowId=?";
    List<Flow> list = createQuery(hql, new Object[] {flowId}).list();
    if (list != null && list.size() > 0) {
      return list.get(0);
    }
    return null;
  }


  public void addFlow(String companyId) throws HibernateException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    String sysDateString = sdf.format(new Date());
    String sql =
        "insert into wf_flow w (w.FLOW_ID,w.FLOW_NAME,w.JBPM_FLOW_ID,w.JBPM_FLOW_KEY,w.CREAT_TIME,w.CREATER,w.COMPANY_ID,w.IS_INHERENT) "
            + " select :companyId||sw.FLOW_ID,sw.FLOW_NAME,sw.JBPM_FLOW_ID,sw.JBPM_FLOW_KEY,TO_DATE('"
            + sysDateString
            + "','YYYY/MM/DD HH24:MI:SS'),sw.CREATER,:companyId,sw.IS_INHERENT "
            + " from sys_wf_flow sw";
    SQLQuery sqlQ = (SQLQuery) createSqlQuery(sql).setString("companyId", companyId);
    sqlQ.executeUpdate();
  }
}
